#ifndef _SENSOR_H
#define _SENSOR_H
#include <stdlib.h>
#include <stdio.h>


//陀螺仪数据
#define IMU_FRAME_HEAD             0x55
#define IMU_FRAME_LEN              11
#define ACC_UPDATE		           0x01
#define GYRO_UPDATE		           0x02
#define ANGLE_UPDATE	           0x04
#define MAG_UPDATE		           0x08
#define READ_UPDATE		           0x80
#define STACKSIZE                  4096
#define RECV_BUF_SIZE              4096
typedef struct __attribute__ ((__packed__))
{
	unsigned char ucYear;
	unsigned char ucMonth;
	unsigned char ucDay;
	unsigned char ucHour;
	unsigned char ucMinute;
	unsigned char ucSecond;
	unsigned int usMiliSecond;
}STime;

struct SAcc
{
	short a[3];
	short T;
};
struct SGyro
{
	short w[3];
	short T;
};
struct SAngle
{
	short Angle[3];
	short T;
};
struct SMag
{
	short h[3];
	short T;
};

struct SDStatus
{
	short sDStatus[4];
};

struct SPress
{
	long lPressure;
	long lAltitude;
};

struct SLonLat
{
	long lLon;
	long lLat;
};

struct SGPSV
{
	short sGPSHeight;
	short sGPSYaw;
	long lGPSVelocity;
};
struct SQ
{ short q[4];
};

typedef struct{
    STime	stcTime;
    struct SAcc 	stcAcc; 
    struct SGyro 	stcGyro; //角加速度
    struct SAngle 	stcAngle;
    struct SMag 	stcMag; //磁场
    struct SDStatus stcDStatus; //端口状态
    struct SPress 	stcPress;
    struct SLonLat 	stcLonLat; //经纬
    struct SGPSV 	stcGPSV; //地速
    struct SQ       stcQ;   //四元素
}Imu_Data;

typedef struct __attribute__ ((__packed__)){
    STime	stcTime;
    float SAcc[3] ;
    float SGyro[3];
    float SAngle[3];
    int  SMag[3];
    int sDStatus[4];
    int SPress;
    float Saltitude;
    int SLonLat[2];
    float SGPSV[3];
    float SQ[4];
}Imu_Sta;

typedef struct __attribute__ ((__packed__)){
    int elec; //电量
    float volt;     //电压
    float curr;     //电流
    int capa;     //额定容量
    float temp;   //温度
    uint8_t curr_dir; //充放电方向
}Battery_Sta;

typedef struct __attribute__ ((__packed__)){
    bool  leak; //漏水检测传感器 
    float temperature; //环境温度传感器
    Battery_Sta battery;//电量传感器
    Imu_Sta imu0;//头部姿态传感器
    Imu_Sta imu1;//重心姿态传感器
}Sensor_Sta;

#define BATTERY_FRAME_HEAD_ONE                0x5A
#define BATTERY_FRAME_HEAD_TWO                0xA5
#define BATTERY_FRAME_LEN_NOCRC               14
#define BATTERY_FRAME_LEN                     15
typedef struct{
    uint8_t frame_header_one;
    uint8_t frame_header_two;
    uint8_t frame_func;
    uint8_t elec;
    uint16_t volt;     //电压
    uint16_t curr;     //电流
    uint16_t capa;     //额定容量
    uint16_t temp;   //温度
    uint8_t curr_dir;  //电流方向
    uint8_t address; //模块地址
    uint8_t  crc8;
}Battery_Frame;

typedef struct{
    uint8_t frame_header;
    uint8_t type;
    uint8_t data[8];
    uint8_t crc8;
}Imu_Frame;

typedef struct {
    uint8_t buffer[RECV_BUF_SIZE];  // 缓冲区存储空间
    size_t head;                       // 头指针，数据入口
    size_t tail;                       // 尾指针，数据出口
    size_t size;                       // 缓冲区总大小
} RingBuffer;

int imu_init(void);

static void Delayms(uint16_t ucMs);
void ring_buffer_init(RingBuffer *rb);
bool ring_buffer_put(RingBuffer *rb, uint8_t data);
bool ring_buffer_get(RingBuffer *rb, uint8_t *data);

static void SensorUartSend(uint8_t *p_data, uint32_t uiSize);
static void CopeSensorData(uint32_t uiReg, uint32_t uiRegNum);
static void uart_cb(const struct device *dev, void *user_data);
void uart_irq_callback(const struct device *dev, void *user_data);
void uart_rx_thread(void *a, void* un1, void* un2);
void imu_rx_dispense(void *p1, void *p2, void *p3);

#endif